package com.pay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pay.dto.BusinessDto;
import com.pay.dto.ResponseEntity;
import com.pay.dto.VerifyDto;
import com.pay.entity.TbBusiness;
import com.pay.mapper.TbBusinessMapper;
import com.pay.service.TbBusinessService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/business")
@Api(tags = "商家入驻")
public class BusinessController {
    @Resource
    TbBusinessService tbBusinessService;
    @Resource
    TbBusinessMapper tbBusinessMapper;

    /**
     * 对于商家注册的用户名和手机号进行验证
     * @param verifyDto
     * @return
     */
    @PostMapping("/checkNameAndPhone")
    @ApiOperation("用户名和手机验证")
    public ResponseEntity checkNameAndPhone(@RequestBody VerifyDto verifyDto){
        return tbBusinessService.checkNameAndPhone(verifyDto);
    }

    /**
     * 对身份证的信息进行验证
     * @param verifyDto
     * @return
     */
    @PostMapping("/checkIdentity")
    @ApiOperation("身份证信息验证")
    public ResponseEntity checkIdentity(@RequestBody VerifyDto verifyDto){
        return tbBusinessService.checkIdentity(verifyDto);
    }

    /**
     * 验证银行卡信息
     * @param verifyDto
     * @return
     */
    @PostMapping("/checkAccount")
    @ApiOperation("银行卡信息验证")
    public ResponseEntity checkAccount(@RequestBody VerifyDto verifyDto){
        return tbBusinessService.checkAccount(verifyDto);
    }

    /**
     * 模拟企查查对营业执照进行验证
     * @param verifyDto
     * @return
     */
    @PostMapping("/checkLicense")
    @ApiOperation("模拟企查查对营业执照进行验证")
    public ResponseEntity checkLicense(@RequestBody VerifyDto verifyDto){
        return tbBusinessService.checkLicense(verifyDto);
    }

    /**
     * 商家注册
     * @param businessDto
     * @return
     */
    @PostMapping("/saveBusiness")
    @ApiOperation("商家注册")
    public ResponseEntity saveBusiness(@RequestBody BusinessDto businessDto){
        return tbBusinessService.saveBusiness(businessDto);
    }

    @GetMapping("/searchBusinessList")
    @ApiOperation("展示管理员未审核的数据")
    public ResponseEntity searchBusinessList(){
        return tbBusinessService.searchBusinessList();
    }

    @PostMapping("/updateState")
    @ApiOperation("管理员审核")
    public ResponseEntity updateState(@RequestBody BusinessDto businessDto, HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        return tbBusinessService.updateState(businessDto,token);
    }

    @PostMapping("/updateMoneyState")
    @ApiOperation("财务人民审核")
    public ResponseEntity updateMoneyState(@RequestBody BusinessDto businessDto,HttpServletRequest request){
        String token = request.getHeader("token");
        return tbBusinessService.updateMoneyState(businessDto,token);
    }

    @PostMapping("/login")
    @ApiOperation("/登录获取token")
    public ResponseEntity login(@RequestBody BusinessDto businessDto){
        QueryWrapper<TbBusiness> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbBusiness::getBusinessName,businessDto.getBusinessName())
                        .eq(TbBusiness::getBusinessPwd,businessDto.getBusinessPwd());
        List<TbBusiness> tbBusinessList = tbBusinessMapper.selectList(wrapper);
        if(tbBusinessList.size()<=0){
            return new ResponseEntity(null,500,"登录失败",false);
        }
        String token=null;
        for (TbBusiness tbBusiness : tbBusinessList) {
            token = Jwts.builder().setId(tbBusiness.getBusinessId() + "").setSubject(tbBusiness.getBusinessName())
                    .signWith(SignatureAlgorithm.HS256, "huangyujie").compact();
        }
        return new ResponseEntity(token,200,"登录成功",true);
    }


    @Test
    public void test(){
        //$2a$10$qyGSR/GOXUIaKrdrAY4wYOTdic.EJe3XyYkWTt7M9uiyqp21FI5au
        String pwd="123456";
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String newPwd = encoder.encode(pwd);
        System.out.println("==============="+newPwd+"=======================");
        boolean flag = encoder.matches(pwd, newPwd);
        System.out.println("=================="+flag+"=====================");
    }

    @Test
    public void test01(){
        System.out.println(Runtime.getRuntime().availableProcessors()); //输出本机CPU的数量,是一个数字
    }

}
